package com.csc.contact.checkbill.utiltools.date;

import com.hollycrm.hollybeacons.system.util.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class DateUtil {
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>();

    private static final Logger logger = Logger.getLogger(DateUtil.class);
    public static DateFormat getDateFormat(String pattern) {
        if (pattern == null || pattern.equals(""))
            pattern = DATE_FORMAT;
        /*DateFormat df = threadLocal.get();
        if (df == null) {
			df = new SimpleDateFormat(pattern);
			threadLocal.set(df);
		}*/
        DateFormat df = new SimpleDateFormat(pattern);
        return df;
    }

    /**
     * 将日期转换为字符串。格式：yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return formatDate(date, null);
    }

    /**
     * 将日期转换为字符串。格式：yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @param pattern 日期格式
     * @return
     */
    public static String formatDate(Date date, String pattern) {
        return getDateFormat(pattern).format(date);
    }

    /**
     * 将字符串日期格式转换为指定的字符串格式日期
     *
     * @param dateStr
     * @param srcPattern 源字符串日期格式
     * @param desPattern 转换后的日期格式
     * @return
     */
    public static String formatDate(String dateStr, String srcPattern, String desPattern) {
        return formatDate(parse(dateStr, srcPattern), desPattern);
    }

    /**
     * 将字符串日期转换为日期。格式：yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return 转换失败，返回null
     */
    public static Date parse(String strDate) {
        return parse(strDate, null);
    }

    /**
     * 将字符串日期转换为日期。格式：yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @param pattern
     * @return
     */
    public static Date parse(String strDate, String pattern) {
        try {
            return getDateFormat(pattern).parse(strDate);
        } catch (ParseException e) {
            logger.info("日期格式化错误===", e);
        }
        return null;
    }

    /**
     * 返回当天时间
     *
     * @param pattern 时间格式，如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getToday(String pattern) {
        if (StringUtils.isNullOrBlank(pattern))
            pattern = DATE_FORMAT;
        return formatDate(new Date(), pattern);
    }


    /**
     * 获取本月第一天
     *
     * @param pattern
     * @return
     */
    public static String getFirstDayOfMonth(String pattern) {
        DateTime now = new DateTime();
        DateTime dTime = new DateTime(now.getYear(), now.getMonthOfYear(), now.dayOfMonth().getMinimumValue(), 0, 0, 0, 0);
        return dTime.toString(pattern);
    }

    /**
     * 获取本月最后一天
     *
     * @param pattern
     * @return
     */
    public static String getLastDayOfMonth(String pattern) {
        DateTime now = new DateTime();
        DateTime dTime = new DateTime(now.getYear(), now.getMonthOfYear(), now.dayOfMonth().getMaximumValue(), 0, 0, 0, 0);
        return dTime.toString(pattern);
    }

    /**
     * 获取某年月的最后一天
     *
     * @return
     */
    public static String getLastDayOfYearMonth(String time) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(time.substring(0, 4)));
        calendar.set(Calendar.MONTH, Integer.parseInt(time.substring(4)) - 1);
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        String lastDayOfMonth = sdf.format(calendar.getTime());
        return lastDayOfMonth;
    }

    /**
     * 返回当天起始时间
     *
     * @return yyyy-MM-dd像我就直说像我就直说像我就直说 00:00:00
     */
    public static String getTodayBegin() {
        return getToday("yyyy-MM-dd 00:00:00");
    }

    /**
     * 返回当天结束时间
     *
     * @return yyyy-MM-dd 23:59:59
     */
    public static String getTodayEnd() {
        return getToday("yyyy-MM-dd 23:59:59");
    }

    /**
     * 返回当天的前几天
     *
     * @param days    当天的前day天
     * @param pattern
     * @return
     */
    public static String getTodayBefore(int days, String pattern) {
        if (StringUtils.isNullOrBlank(pattern))
            pattern = DATE_FORMAT;
        TimeZone zone = TimeZone.getTimeZone("GMT+08:00");
        Calendar calendar = Calendar.getInstance(zone);
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, days);
        return formatDate(calendar.getTime(), pattern);
    }

    /**
     * 返回当天的前几个月
     *
     * @param month
     * @param pattern
     * @return
     */
    public static String getTodayBeforeMonth(int month, String pattern) {
        if (StringUtils.isNullOrBlank(pattern))
            pattern = DATE_FORMAT;
        TimeZone zone = TimeZone.getTimeZone("GMT+08:00");
        Calendar calendar = Calendar.getInstance(zone);
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, month);
        return formatDate(calendar.getTime(), pattern);
    }

    /**
     * 返回当天时间
     *
     * @return Date类型日期
     */
    public static Date getToday() {
        TimeZone zone = TimeZone.getTimeZone("GMT+08:00");
        Calendar calendar = Calendar.getInstance(zone);
        calendar.setTime(new Date());
        return calendar.getTime();
    }

    /**
     * 计算两个日期相差的月份
     *
     * @param date1 前一个日期
     * @param date2 后一个日期
     * @return
     * @throws ParseException
     */
    public static int getMonthSpace(String date1, String date2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));
            int month = (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12 + (c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH));
            return month;
        } catch (ParseException e) {
            logger.info("获取日期月份错误===", e);
        }
        return 0;
    }

    /**
     * 比较两个日期
     *
     * @param subActiveTime 前一个日期
     * @param nowMonth      后一个日期
     * @param str           格式
     * @return
     * @throws ParseException
     */
    public static int compareToTime(String subActiveTime, String nowMonth, String str) {
        DateFormat df = new SimpleDateFormat(str);
        //获取Calendar实例
        Calendar currentTime = Calendar.getInstance();
        Calendar compareTime = Calendar.getInstance();

        //把字符串转成日期类型
        try {
            currentTime.setTime(df.parse(nowMonth));
            compareTime.setTime(df.parse(subActiveTime));
        } catch (ParseException e) {
            logger.info("日期比较化错误===", e);
        }
        //利用Calendar的方法比较大小
        if (currentTime.compareTo(compareTime) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 返回上个月
     *
     * @param nowMonth 当前月份
     * @return
     * @throws ParseException
     */
    public static String queryMonth(String nowMonth) {
        Date date = parse(nowMonth, "yyyyMM");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        Date OldDate = cal.getTime();
        return formatDate(OldDate, "yyyyMM");


    }

    /**
     * 获取当前月份
     *
     * @return
     */
    public static String getNowMonth() {
        return formatDate(new Date(), "yyyyMM");
    }

    private static Date getLastDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        return cal.getTime();
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static String year() {
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    //获取东八区时间
        //获取年
        return String.valueOf(c.get(Calendar.YEAR));
    }

    /**
     * 获取当前月
     *
     * @return
     */
    public static String month() {
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    //获取东八区时间
        //获取年
        return formatDate(new Date(), "MM");
    }

    /**
     * 获取当前天
     *
     * @return
     */
    public static String day() {
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    //获取东八区时间
        //获取年
        return String.valueOf(c.get(Calendar.DAY_OF_MONTH));
    }

}